package com.qianbao.controller.v1.money;


import com.qianbao.components.excepion.CommonException;
import com.qianbao.components.svresut.Result;
import com.qianbao.components.svversion.ApiVersion;
import com.qianbao.service.coinrecord.CoinRecordService;
import com.qianbao.service.money.CoinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;


@Controller
@ApiVersion(1)
@RequestMapping("/moneycenter/coin/{version}/")
public class CoinController {

    @Autowired
    private CoinService coinService;

    @Autowired
    private CoinRecordService coinRecordService;



    @RequestMapping("/save-coin-type")
    @ResponseBody
    public Result saveUserCoinWithType(String customId,String type){
        try {
            coinService.saveUserCoinWithType(customId,type);
            return Result.success(Collections.emptyMap());
        } catch (CommonException e) {
            e.printStackTrace();
            return Result.fail(e.getCode(),e.getMessage());
        }
    }



    @RequestMapping("/save-coin")
    @ResponseBody
    public Result saveUserCoin(String customId, String coinNumber){

        try {
            coinService.saveUserCoin(customId,coinNumber);
            return Result.success(Collections.emptyMap());
        } catch (CommonException e) {
            e.printStackTrace();
            return Result.fail(e.getCode(),e.getMessage());
        }

    }


    @RequestMapping("/save-coin-and-tips")
    @ResponseBody
    public Result saveUserCoinAndTips(String customId, String coinNumber,String tips){
        try {
            coinService.saveUserCoin(customId,coinNumber);
            coinRecordService.saveCoinRecord(customId,coinNumber,tips);
            return Result.success(Collections.emptyMap());
        } catch (CommonException e) {
            e.printStackTrace();
            return Result.fail(e.getCode(),e.getMessage());
        }
    }



    @RequestMapping("/find-coin")
    @ResponseBody
    public Result findUserCoin(String customId){

        try {
            int coin = coinService.findUserCoin(customId);
            HashMap<String, String> map = new HashMap<>();
            map.put("coin",String.valueOf(coin));
            return Result.success(map);
        } catch (CommonException e) {
            e.printStackTrace();
            return Result.fail(e.getCode(),e.getMessage());
        }

    }



    @RequestMapping("/exchange-readtime-coin")
    @ResponseBody
    public Result exchangeReadTimeToCoin(String customId){

        try {
            Map map = coinService.exchangeReadtimeToCoin(customId);
            return Result.success(map);
        } catch (CommonException e) {
            e.printStackTrace();
            return Result.fail(e.getCode(),e.getMessage());
        }

    }





}
